local skynet = require "skynet"
local configdao = require "configdao"
local tabletool = require "tabletool"
local max_client = 100000

skynet.start(function()
	print("Server start")
	skynet.newservice("systemlog")
    local confcentersvr = skynet.newservice("confcenter")
    skynet.call(confcentersvr, "lua", "start")
    print("confcenter start success")

    local gmsvrs = configdao.get_svrs("gmsvrs")
    if gmsvrs == nil then
        print("gmsvrd start failed gmsvrs == nil")
        skynet.exit()
    end
    local gmsvr = gmsvrs[skynet.getenv("svr_id")]
    if gmsvr == nil then
        print("gmsvrd start failed gmsvr == nil", skynet.getenv("svr_id"))
        skynet.exit()           
    end

    local mongologs = configdao.get_svrs("mongologs")
    if mongologs ~= nil then
        for id, conf in pairs(mongologs) do
            local svr = skynet.newservice("mongolog", id)
            skynet.call(svr, "lua", "init", conf)            
        end
    end

	skynet.newservice("debug_console",gmsvr.debug_console_port)

    local params = ",,,,,"..skynet.getenv("svr_id")   
	local watchdog = skynet.newservice("gmsvrd", params)

    local proxys = configdao.get_svrs("proxys")
    if proxys ~= nil then
        local tmpconf
        for id, conf in pairs(proxys) do
            local svr = skynet.uniqueservice("proxy", id)
            tmpconf = tabletool.deepcopy(conf)
            tmpconf.svr_id = skynet.getenv("svr_id")
            skynet.call(svr, "lua", "init", tmpconf)            
        end 
    end

	skynet.call(watchdog, "lua", "cmd", "start", {
		maxclient = max_client,
		nodelay = true,
		agentsize = gmsvr.agentsize,
        agentincr = gmsvr.agentincr,
        gmhttpsvr_ip = gmsvr.gmhttpsvr_ip,
        gmhttpsvr_port = gmsvr.gmhttpsvr_port,
	})
	print("gmsvrd listen on ")
	skynet.exit()	
end)
